/**
 *
 */
package nz.ac.massey.jfontview.example;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JTextPane;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;

import nz.ac.massey.jfontview.FontList;

/**
 * @author Carl Robinson
 * @version 0.1
 */
public class MenuEvent implements ActionListener {

	/* (non-Javadoc)
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	public void actionPerformed(ActionEvent ae) {
		String command = ae.getActionCommand();
		if (command.equals("newdoc")){
			System.out.println(command);
		}
		else if (command.equals("open")){
			System.out.println(command);
		}
		else if (command.equals("close")){
			System.out.println(command);
		}
		else if (command.equals("save")){

		}
		else if (command.equals("saveas")){
			JTextPane editor = EditorUI.getEditor();
			int returnVal = EditorUI.fc.showSaveDialog(null);
			if (returnVal == JFileChooser.APPROVE_OPTION){
				File file = EditorUI.fc.getSelectedFile();
				if (file.exists ()) {
		             int response = JOptionPane.showConfirmDialog (null,
		               "Overwrite existing file?","Confirm Overwrite",
		                JOptionPane.OK_CANCEL_OPTION,
		                JOptionPane.QUESTION_MESSAGE);
		             if (response != JOptionPane.CANCEL_OPTION){
		            	 writeFile (file, editor.getText ());
		             }
		         }
				else{
					writeFile (file, editor.getText ());
				}


			}
		}
		else if (command.equals("exit")){
			//TODO save the list of favourites to the filesystem
			System.exit(0);
		}
		/**
		 * Event for the edit font menu item
		 */
		else if (command.equals("editfont")){
			JTextPane editor = EditorUI.getEditor();
			FontList favourites = EditorUI.getFavourites();
			/**
			 * Instansiate the FontDialog and return the selected font
			 */
			FontDialog dialog;
			if(favourites != null){
				dialog = new FontDialog(EditorUI.getFrame(), favourites);
			}
			else{
				dialog = new FontDialog(EditorUI.getFrame());
			}
			Font newFont = dialog.open();
			EditorUI.setFavourites(dialog.getFavouriteFonts());

			/**
			 * Start with the current input attributes for the JTextPane. This
	         * should ensure that we do not wipe out any existing attributes
	         * (such as alignment or other paragraph attributes) currently
	         * set on the text area.
			 */
			MutableAttributeSet atrib = editor.getInputAttributes();

			/**
			 * backs up the old atributes
			 */
//			MutableAttributeSet oldAtrib = atrib;

			/**
			 * Set the font family, size, and style, based on properties of
	         * the Font object. Note that JTextPane supports a number of
	         * character attributes beyond those supported by the Font class.
	         * For example, underline, strike-through, super- and sub-script.
	         */
			if(newFont!=null){
		        StyleConstants.setFontFamily(atrib, newFont.getFamily());
		        StyleConstants.setFontSize(atrib, newFont.getSize());
		        StyleConstants.setItalic(atrib, (newFont.getStyle() & Font.ITALIC) != 0);
		        StyleConstants.setBold(atrib, (newFont.getStyle() & Font.BOLD) != 0);
			}

			String text = editor.getSelectedText();
			StyledDocument doc = editor.getStyledDocument();

			if (text  == null){
				/**
				 * Apply font to any text typed afterwards
				 */
				int caret = editor.getCaretPosition();
				doc.setCharacterAttributes(caret, 0, atrib, true);
			}
			else{
				/**
				 * Apply font to selected text
				 */
				int selStart = editor.getSelectionStart();
				int selEnd = editor.getSelectionEnd();
				doc.setCharacterAttributes(selStart, (selEnd-selStart), atrib, true);
			}
		}
		else{
			System.out.println("Unknown Action Command: "+ command);
		}


	}

	public boolean writeFile (File file, String dataString) {
	    try {
	       PrintWriter out = new PrintWriter (new BufferedWriter (new FileWriter (file)));
	       out.print (dataString);
	       out.flush ();
	       out.close ();
	    }
	    catch (IOException e) {
	       return false;
	    }
	    return true;
	  } // writeFile




}
